package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.ApplicationForm;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by liyulin on 2016/5/16.
 */
public class ApplicationFormConvert {
    public static DBObject castApplicationFormToDBOjbect(ApplicationForm applicationForm) {
        DBObject dbObject = new BasicDBObject();
        if (applicationForm.getId() != null) {
            dbObject.put("_id", applicationForm.getId());
        }
        if (applicationForm.getActivityId() != null) {
            dbObject.put("activityId", applicationForm.getActivityId());
        }
        if (applicationForm.getUserId() != null) {
            dbObject.put("userId", applicationForm.getUserId());
        }
        if (StringUtils.isNotBlank(applicationForm.getName())) {
            dbObject.put("name", applicationForm.getName());
        } else {
            dbObject.put("name", "");
        }
        if (applicationForm.getGender() != null) {
            dbObject.put("gender", applicationForm.getGender());
        } else {
            dbObject.put("gender", 0);
        }
        if (StringUtils.isNotBlank(applicationForm.getIdNo())) {
            dbObject.put("idNo", applicationForm.getIdNo());
        } else {
            dbObject.put("idNo", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getTelephone())) {
            dbObject.put("telephone", applicationForm.getTelephone());
        } else {
            dbObject.put("telephone", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getBirthday())) {
            dbObject.put("birthday", applicationForm.getBirthday());
        } else {
            dbObject.put("birthday", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCity())) {
            dbObject.put("city", applicationForm.getCity());
        } else {
            dbObject.put("city", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getHomeAddress())) {
            dbObject.put("homeAddress", applicationForm.getHomeAddress());
        } else {
            dbObject.put("homeAddress", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCooperation())) {
            dbObject.put("cooperation", applicationForm.getCooperation());
        } else {
            dbObject.put("cooperation", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCoWebsite())) {
            dbObject.put("coWebsite", applicationForm.getCoWebsite());
        } else {
            dbObject.put("coWebsite", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getDepartment())) {
            dbObject.put("department", applicationForm.getDepartment());
        } else {
            dbObject.put("department", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getTitle())) {
            dbObject.put("title", applicationForm.getTitle());
        } else {
            dbObject.put("title", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCoTelephone())) {
            dbObject.put("coTelephone", applicationForm.getCoTelephone());
        } else {
            dbObject.put("coTelephone", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCoFax())) {
            dbObject.put("coFax", applicationForm.getCoFax());
        } else {
            dbObject.put("coFax", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getCoAddress())) {
            dbObject.put("coAddress", applicationForm.getCoAddress());
        } else {
            dbObject.put("coAddress", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getEmail())) {
            dbObject.put("email", applicationForm.getEmail());
        } else {
            dbObject.put("email", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getQq())) {
            dbObject.put("qq", applicationForm.getQq());
        } else {
            dbObject.put("qq", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getWeixin())) {
            dbObject.put("weixin", applicationForm.getWeixin());
        } else {
            dbObject.put("weixin", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getSelfIntroduction())) {
            dbObject.put("selfIntroduction", applicationForm.getSelfIntroduction());
        } else {
            dbObject.put("selfIntroduction", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getHobbies())) {
            dbObject.put("hobbies", applicationForm.getHobbies());
        } else {
            dbObject.put("hobbies", "");
        }
        if (StringUtils.isNotBlank(applicationForm.getMemo())) {
            dbObject.put("memo", applicationForm.getMemo());
        } else {
            dbObject.put("memo", "");
        }
        if (applicationForm.getCreateTime() != null) {
            dbObject.put("createTime", ObjUtils.toLong(applicationForm.getCreateTime()));
        }

        return dbObject;
    }

    public static ApplicationForm castDBObjectToApplicationForm(DBObject dbObject) {
        ApplicationForm applicationForm = new ApplicationForm();
        if (dbObject != null) {
            applicationForm.setId(ObjectUtils.toLong(dbObject.get("_id")));
            applicationForm.setActivityId(ObjectUtils.toLong(dbObject.get("activityId")));
            applicationForm.setUserId(ObjectUtils.toLong(dbObject.get("userId")));
            applicationForm.setName(ObjectUtils.toString(dbObject.get("name")));
            applicationForm.setGender(ObjectUtils.toInteger(dbObject.get("gender")));
            applicationForm.setIdNo(ObjectUtils.toString(dbObject.get("idNo")));
            applicationForm.setTelephone(ObjectUtils.toString(dbObject.get("telephone")));
            applicationForm.setBirthday(ObjectUtils.toString(dbObject.get("birthday")));
            applicationForm.setCity(ObjectUtils.toString(dbObject.get("city")));
            applicationForm.setHomeAddress(ObjectUtils.toString(dbObject.get("homeAddress")));
            applicationForm.setCooperation(ObjectUtils.toString(dbObject.get("cooperation")));
            applicationForm.setCoWebsite(ObjectUtils.toString(dbObject.get("coWebsite")));
            applicationForm.setDepartment(ObjectUtils.toString(dbObject.get("department")));
            applicationForm.setTitle(ObjectUtils.toString(dbObject.get("title")));
            applicationForm.setCoTelephone(ObjectUtils.toString(dbObject.get("coTelephone")));
            applicationForm.setCoFax(ObjectUtils.toString(dbObject.get("coFax")));
            applicationForm.setCoAddress(ObjectUtils.toString(dbObject.get("coAddress")));
            applicationForm.setEmail(ObjectUtils.toString(dbObject.get("email")));
            applicationForm.setQq(ObjectUtils.toString(dbObject.get("qq")));
            applicationForm.setWeixin(ObjectUtils.toString(dbObject.get("weixin")));
            applicationForm.setSelfIntroduction(ObjectUtils.toString(dbObject.get("selfIntroduction")));
            applicationForm.setHobbies(ObjectUtils.toString(dbObject.get("hobbies")));
            applicationForm.setMemo(ObjectUtils.toString(dbObject.get("memo")));
            applicationForm.setCreateTime(ObjectUtils.toLong(dbObject.get("createTime")));
        }
        return applicationForm;
    }
}
